//
//  RHMePolicyPatternDetailVC.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/22.
//

#import "RHMePolicyPatternDetailVC.h"
#import "RHMePolicyPatternDetailCell.h"
#import "RHPatternModel.h"
#import "RHMePolicyModel.h"
#import "SYChartTool.h"

@interface RHMePolicyPatternDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) NSArray<RHMePolicyModel*> *dataArr;

@end

@implementation RHMePolicyPatternDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"模板详情".rh_localized;
    self.view.backgroundColor = CFTabViewBgColor;
    [self loadData];
    [self setupUI];
    
}



- (void)setupUI{
    kWeakSelf;
    
    QMUIButton *sureBtn = [QMUIButton buttonWithTitle:@"使用模板".rh_localized titleColor:UIColor.whiteColor titleFont:kBOLDSYSTEMFONT(20)];
    [sureBtn setCornerRadius:8];
    [sureBtn setBackgroundColor:CMainColorGreen];
    [self.view addSubview:sureBtn];
    [sureBtn addTarget:self action:@selector(userPatternAction) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(50);
        make.bottom.mas_offset(kSafe_bottom>0?-kSafe_bottom:-15);
    }];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:kCOLOR_HEX(@"#F0F5FF")];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_tableView setSeparatorColor:UIColor.clearColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[RHMePolicyPatternDetailCell class] forCellReuseIdentifier:@"RHMePolicyPatternDetailCell"];
   
    
    [self.view addSubview:_tableView];
   
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.bottom.equalTo(sureBtn.mas_top).offset(-15);
        make.top.mas_offset(0);
    }];

}

-(void)loadData
{
    //先加配置参数
    NSArray *arr = [SYChartTool loadJsonFileWithName:@"me_policy_config"];
    self.dataArr = [NSArray yy_modelArrayWithClass:[RHMePolicyModel class] json:arr];
   
    
    NSArray *dataArr = [self.patternModel.configJson jsonStringToArray];
    
    NSMutableArray *rowArr = [NSMutableArray array];
    [self.dataArr enumerateObjectsUsingBlock:^(RHMePolicyModel*  _Nonnull sectionModel, NSUInteger idx, BOOL * _Nonnull stop) {
        //只对时间段操作
        if ([sectionModel.type isEqualToString:@"time"]) {
            RHMePolicyModel *rowModel = [sectionModel.rows firstObject];
            RHMePolicyModel *chgTagModel = [rowModel.items safeObjectAtIndex:0];
            RHMePolicyModel *socModel = [rowModel.items safeObjectAtIndex:1];
            RHMePolicyModel *powerModel = [rowModel.items safeObjectAtIndex:2];
            
            NSInteger policyCount = self.patternModel.segmentNum==0?1:self.patternModel.segmentNum;
            NSInteger timeRangeCount = 24 / policyCount;
            //生成相应时间段
            for (int i = 0; i <policyCount; i++) {
                NSDictionary *dataDic = [dataArr safeObjectAtIndex:i];
                NSInteger start_hour = i==0? 0 : i * timeRangeCount ;
                NSInteger start_minute = 0;
                
                NSInteger end_hour = (i+1) * timeRangeCount;
                NSInteger end_minute = 0 ;
                
                NSDictionary *paraDic = [dataArr safeObjectAtIndex:i];
                RHMePolicyModel *rowNewModel = [[RHMePolicyModel alloc]init];
                rowNewModel.type = rowModel.type;
                rowNewModel.comment = [NSString stringWithFormat:@"%02ld:%02ld-%02ld:%02ld",start_hour,start_minute,end_hour,end_minute];
                
                NSMutableArray *items = [NSMutableArray array];
                RHMePolicyModel *chgTagNewModel = [[RHMePolicyModel alloc]init];
                chgTagNewModel.selectList = chgTagModel.selectList.copy;
                chgTagNewModel.key     = chgTagModel.key;
                chgTagNewModel.format  = chgTagModel.format;
                chgTagNewModel.type    = chgTagModel.type;
                chgTagNewModel.regAddr = chgTagModel.regAddr;
                chgTagNewModel.offset  = chgTagModel.offset;
                chgTagNewModel.num     = chgTagModel.num;
                //状态
                if ([dataDic[@"status"] isEqualToString:@"charge"]) {
                    chgTagNewModel.content = @"1";
                }else if([dataDic[@"status"] isEqualToString:@"discharge"]){
                    chgTagNewModel.content = @"2";
                }else{
                    chgTagNewModel.content = @"0";
                }
                
                RHMePolicyModel *socNewModel = [[RHMePolicyModel alloc]init];
                socNewModel.key     = [NSString stringWithFormat:@"%@%d",socModel.key,i+1];
                socNewModel.format  = socModel.format;
                socNewModel.type    = socModel.type;
                socNewModel.regAddr = socModel.regAddr + i;
                socNewModel.offset  = socModel.offset;
                socNewModel.num     = socModel.num;
                socNewModel.content = ValidNullDataToString(dataDic[@"soc"], @"0");
                
                RHMePolicyModel *powerNewModel = [[RHMePolicyModel alloc]init];
                powerNewModel.key     = [NSString stringWithFormat:@"%@%d",powerModel.key,i+1];
                powerNewModel.format  = powerModel.format;
                powerNewModel.type    = powerModel.type;
                powerNewModel.regAddr = powerModel.regAddr + i;
                powerNewModel.offset  = powerModel.offset;
                powerNewModel.num     = powerModel.num;
                powerNewModel.content = ValidNullDataToString(paraDic[@"power"],@"0");
                
                [items addObject:chgTagNewModel];
                [items addObject:socNewModel];
                [items addObject:powerNewModel];
                rowNewModel.items = items.copy;
                [rowArr addObject:rowNewModel];
            }
            sectionModel.rows = rowArr.copy;
        }
    }];
    //去掉工作模式,只取时间段
    self.dataArr = [NSArray arrayWithObject:[self.dataArr lastObject]];
}

-(void)userPatternAction
{
    if (self.userPatternBlock) {
        self.userPatternBlock(self.patternModel);
        [self.navigationController popViewControllerAnimated:NO];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    RHMePolicyModel *sectionModel = self.dataArr[section];
    return sectionModel.rows.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RHMePolicyPatternDetailCell *policyCell = [tableView dequeueReusableCellWithIdentifier:@"RHMePolicyPatternDetailCell"];
    policyCell.selectionStyle = UITableViewCellSelectionStyleNone;
    RHMePolicyModel *sectionModel = self.dataArr[indexPath.section];
    RHMePolicyModel *rowModel = sectionModel.rows[indexPath.row];
    rowModel.rowTitle = [NSString stringWithFormat:@"%@ %ld",@"时段".rh_localized,indexPath.row+1];
    policyCell.model = rowModel;
    return policyCell;
}


@end
